package com.fy56.platform.logistics.restful.api.controller.gmms;


import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.service.wx.IGmmsMiniProgramService;
import com.fy56.platform.logistics.utils.ParamUtil;
import com.fy56.platform.logistics.utils.StringUtil;
import com.wordnik.swagger.annotations.Api;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/*
 * @项目名称：
 * @类名称：GmmsMiniProgramController
 * @类描述：
 * @创建人：
 * @作者单位：哈尔滨飞扬软件技术有限公司
 * --------------------------------------
 * @创建时间：2019-04-16 16:54:15
 * @version：1.0.0
 */
@Api(value = "GmmsMiniProgramController", description = "仓配发货小程序-微信工具类", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/gmmsMiniProgramController")
public class GmmsMiniProgramController {

    // 日志工具类
    private static Logger logger = Logger.getLogger(GmmsMiniProgramController.class);

    @Autowired
    private IGmmsMiniProgramService iGmmsMiniProgramService;


    /**
     * 获取小程序OpenId信息
     *
     * http://192.168.3.252:8082/logistics-restful-api/gmmsMiniProgramController/getOpenId
     * data = {"code":"123456","url":"http://192.168.3.220:2222/gmms_web/","companyId":""}
     */
    @RequestMapping(value = "/getOpenId")
    @ResponseBody
    public ResponseEntity getOpenId(@RequestParam String data){ResultModel result = null;
        Long begin = System.currentTimeMillis();    // 开始时间
        try{
            if(StringUtil.isNotEmpty(data)) {
                Map<String, Object> paramMap = JSON.parseObject(data);
                String paramsErrorMsg = "";
                String code = ParamUtil.getStringValue(paramMap.get("code"));
                if (StringUtil.isEmpty(code)) {
                    paramsErrorMsg = "code参数不可为空 ";
                }
                String url = ParamUtil.getStringValue(paramMap.get("url"));
                if (StringUtil.isEmpty(url)) {
                    paramsErrorMsg += "url参数不可为空 ";
                }
                String companyId = ParamUtil.getStringValue(paramMap.get("companyId"));
                // 获取openId 与用户信息
                if (StringUtil.isEmpty(paramsErrorMsg)) {
                    result = iGmmsMiniProgramService.getOpenIdService(code, url, companyId);
                } else {
                    result = new ResultModel(101, paramsErrorMsg, null);
                }
            }else{
                result = new ResultModel(101, "获得gmms微信小程序openId信息：参数不可为空。",null);
            }
        }catch(Exception ex){
            result = new ResultModel(101, "获得gmms微信小程序openId信息-getOpenId：" + ex.getMessage(),null);
            logger.error("获得gmms微信小程序openId信息-getOpenId：",ex);
        }finally {
            Long endTime = System.currentTimeMillis();
            logger.info("MethodName: getOpenId方法");
            logger.info("Data: " + data);
            logger.info("result: " + ((result==null)?"": JSON.toJSONString(result)));
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


}
